package com.tang.watermake;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.time.Duration;

/**
 * 间隔联结带迟到数据处理
 * 1.只支持事件时间
 * 2.指定上界、下界的偏移，负号代表时间往前，正号代表时间往后
 * 3.process中，只能处理join上的数据
 * 4.两条流关联后的watermark，以两条流中最小的为准，和前面的一致
 * 5.如果当前数据的事件时间 < 当前的watermark，就是迟到数据，主流的process不处理
 *  => between后，可以指定将左流或右流的迟到数据放入侧输出流
 *
 * @author tang
 * @since 2023/6/30 23:47
 */
public class IntervalJoinWithLateDemo {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        SingleOutputStreamOperator<Tuple2<String, Integer>> dataStream1 = env
                .socketTextStream("192.168.70.141", 7777)
                .map(new MapFunction<String, Tuple2<String, Integer>>() {
                    @Override
                    public Tuple2<String, Integer> map(String value) throws Exception {
                        String[] split = value.split(",");
                        return Tuple2.of(split[0], Integer.valueOf(split[1]));
                    }
                })
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<Tuple2<String, Integer>>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner((element, recordTimestamp) -> element.f1 * 1000L)
                );

        SingleOutputStreamOperator<Tuple3<String, Integer, Integer>> dataStream2 = env
                .socketTextStream("192.168.70.141", 8888)
                .map(new MapFunction<String, Tuple3<String, Integer, Integer>>() {
                    @Override
                    public Tuple3<String, Integer, Integer> map(String value) throws Exception {
                        String[] split = value.split(",");
                        return Tuple3.of(split[0], Integer.valueOf(split[1]), Integer.valueOf(split[2]));
                    }
                })
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<Tuple3<String, Integer, Integer>>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner((element, recordTimestamp) -> element.f1 * 1000L)
                );

        KeyedStream<Tuple2<String, Integer>, String> keyedStream1 = dataStream1.keyBy(new KeySelector<Tuple2<String, Integer>, String>() {
            @Override
            public String getKey(Tuple2<String, Integer> value) throws Exception {
                return value.f0;
            }
        });
        KeyedStream<Tuple3<String, Integer, Integer>, String> keyedStream2 = dataStream2.keyBy(r2 -> r2.f0);

        OutputTag<Tuple2<String, Integer>> k1LateTag = new OutputTag<>("key1-late", Types.TUPLE(Types.STRING, Types.INT));
        OutputTag<Tuple3<String, Integer, Integer>> k2LateTag = new OutputTag<>("key2-late", Types.TUPLE(Types.STRING, Types.INT, Types.INT));
        SingleOutputStreamOperator<String> process = keyedStream1.intervalJoin(keyedStream2)
                .between(Time.seconds(-2), Time.seconds(2))
                .sideOutputLeftLateData(k1LateTag)
                .sideOutputRightLateData(k2LateTag)
                .process(new ProcessJoinFunction<Tuple2<String, Integer>, Tuple3<String, Integer, Integer>, String>() {

                    /**
                     * 两条流的数据匹配上才会进入这个方法
                     *
                     * @param left The left element of the joined pair. 左流的element
                     * @param right The right element of the joined pair. 右流的element
                     * @param ctx A context that allows querying the timestamps of the left, right and joined pair.
                     *     In addition, this context allows to emit elements on a side output.
                     *     上下文工具，获取信息用，前面提到过哦
                     * @param out The collector to emit resulting elements to. 采集器输出用
                     * @throws Exception 异常
                     */
                    @Override
                    public void processElement(Tuple2<String, Integer> left, Tuple3<String, Integer, Integer> right, ProcessJoinFunction<Tuple2<String, Integer>, Tuple3<String, Integer, Integer>, String>.Context ctx, Collector<String> out) throws Exception {
                        // 进入这个方法时关联上的数据
                        out.collect(left + "<======>" + right);
                    }
                });
        
        process.getSideOutput(k1LateTag).print("左流");
        process.getSideOutput(k2LateTag).print("右流");
        process.print("主流");


        env.execute();
    }

}
